﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Sockets;
using _2DgameServer.Tool;
using _2DgameServer.MysqlData;
using NetgameProtocol;
using MySql.Data.MySqlClient;

namespace _2DgameServer.Servers
{
    class Client
    {
        private string connstr = "Database=game;Data source=127.0.0.1;port=3306;User Id=root;Password=123456;";

        private Socket socket;
        private Message message;
        private UserData userData;
        private Server server;
        private MySqlConnection sqlConnection;

        public UserInFo GetUserInFo { get; set; }
        public class UserInFo
        {
            public string UserName { get; set; }
            public int HP { get; set; }
            public PosPack Pos { get; set; }
        }
        public Room GetRoom { get; set; }

        public string UserName { get; set; }
        public UserData GetUserData
        {
            get { return userData; }
        }
        public MySqlConnection GetMySqlConnect
        {
            get { return sqlConnection; }
        }

        public Client(Socket socket, Server server)
        {
            message = new Message();
            userData = new UserData();

            sqlConnection = new MySqlConnection(connstr);
            GetUserInFo = new UserInFo();

            sqlConnection.Open();

            this.server = server;
            this.socket = socket;

            //message = new Message();
            StartReceive();

        }
        //开始接收
        void StartReceive()
        {
            socket.BeginReceive(message.Buffer, message.StartIndex, message.Remsize, SocketFlags.None, ReceiveCallback, null);

        }
        void ReceiveCallback(IAsyncResult iar)
        {
            try
            {
                if (socket == null || socket.Connected == false) return;
                int len = socket.EndReceive(iar);
                if (len == 0)
                {
                    Close();
                    return;
                }
                //解析消息，然后根据解析出来的的消息通过委托形式调用所需的请求方法
                message.ReadBuffer(len, HandleRequest);
                StartReceive();
            }
            catch
            {
                Close();
            }

        }
        //发送消息
        public void Send(MainPack pack)
        {
            socket.Send(Message.PackData(pack));
        }
        void HandleRequest(MainPack pack)
        {
            server.HandleRequest(pack, this);
        }
        //服务器处理注册
        /*        public bool Logon( MainPack pack)
                {
                    return GetUserData.Logon(pack,sqlConnection);
                }*/

        private void Close()
        {
            if(GetRoom != null)
            {
                GetRoom.Exit(server,this);
            }
            server.RemoveClient(this);
            socket.Close();
            sqlConnection.Close();
        }
        public void UpPos(MainPack pack)
        {
            GetUserInFo.Pos = pack.Playerpack[0].Pospack;
        }
    }
}
